package com.example.demo.controller;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.service.impl.UserServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.util.List;

/**
 * 用户信息的控制器
 */
@RestController
public class UserController {
    //方式一：属性注入@Autowired//自动注入(Resource)不会报波浪线
    // private UserService userService;
    //方式二：构造器注入
//    private final UserService userService;
//    @Autowired
//    public UserController(UserService userService) {
//        this.userService = userService;
//    }
    //方式三：set注入
    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    // @RequestMapping("/list")
   // public List<User> list() throws FileNotFoundException {
        //1.加载并读取user.txt文件
        //借助hutool工具类读文件-一行一行读数据
        //InputStream in=new FileInputStream("D:\\IDEA\\WorkSpace\\web-ai-demo1\\springboot-web-01\\src\\main\\resources\\user.txt");不推荐绝对文件路径
        //InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");//获取文件的路径
       // ArrayList<String> lines=IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>());
        //2.将读取到的数据封装到User对象中--封装到List集合中
//        List<User> userList=lines.stream().map(line -> {
//            String[] parts=line.split(",");
//            Integer id=Integer.parseInt(parts[0]);
//            String username=parts[1];
//            String password=parts[2];
//            String name=parts[3];
//            Integer age=Integer.parseInt(parts[4]);
//            LocalDateTime updateTime=LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//            return new User(id,username,password,name,age,updateTime);
//        }).toList();//转为List集合
        //3.将List集合以JSON格式返回
        //return userList;
   // }
    @RequestMapping("/list")
    public List<User> list() throws FileNotFoundException {
       //1.调用service，查询所有用户信息
        List<User> userList=userService.findAll();
        //2.返回数据
        return userList;
    }
}
